package com.itheim.controller;

import com.itheim.anno.Log;
import com.itheim.pojo.Emp;
import com.itheim.pojo.EmpQueryParam;
import com.itheim.pojo.PageResult;
import com.itheim.pojo.Result;
import com.itheim.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;
    // 分页查询
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询,参数:{}",empQueryParam);
        PageResult<Emp> pageResult =empService.page(empQueryParam);
        return Result.success(pageResult);
    }
    // 新增员工
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工,员工信息:{}",emp);
        empService.save(emp);
        return Result.success();
    }
    // 员工删除
    @Log
    @DeleteMapping
    public Result delete(Integer[] ids) {
        log.info("根据id删除员工,id:{}",ids);
        empService.delete(ids);
        return Result.success();
    }
    //根据id查询信息
    @GetMapping("/{id}")
    public Result getinfo(@PathVariable Integer id) {
        log.info("根据id查询员工信息,id:{}",id);
        Emp emp = empService.getinfo(id);
        return Result.success(emp);
    }
    //更新员工
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工信息,员工信息:{}",emp);
        empService.update(emp);
        return Result.success();
    }
    // 查询全部员工
    @GetMapping("/list")
    public Result list() {
        log.info("查询全部员工");
        List<Emp> emplist=empService.list();
        return Result.success(emplist);
    }


}
